package com.heima.home.controller;

import com.heima.home.dto.ReviewsDTO;
import com.heima.home.entity.Reviews;
import com.heima.home.exception.CustomException;
import com.heima.home.service.ReviewsService;
import com.heima.home.util.UserContext;
import com.heima.home.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/reviews")
public class ReviewsController {
    @Autowired
    private ReviewsService reviewsService;

    @PostMapping("/addReviews")
    public Result<Void> addReviews(@RequestBody ReviewsDTO reviewsDTO){
        // 参数校验
        if (reviewsDTO == null) {
            throw new CustomException("评价信息不能为空");
        }
        if (reviewsDTO.getBookingId() == null) {
            throw new CustomException("订单ID不能为空");
        }
        if (reviewsDTO.getRating() == null || reviewsDTO.getRating() < 1 || reviewsDTO.getRating() > 5) {
            throw new CustomException("评分必须在1-5之间");
        }
        if (reviewsDTO.getContent() == null || reviewsDTO.getContent().trim().isEmpty()) {
            throw new CustomException("评价内容不能为空");
        }
        if (reviewsDTO.getContent().length() < 10 || reviewsDTO.getContent().length() > 500) {
            throw new CustomException("评价内容长度必须在10-500个字符之间");
        }
        
        Integer currentUserId = UserContext.getCurrentUserId();
        reviewsDTO.setUserId(currentUserId);
        reviewsDTO.setCreatedAt(LocalDateTime.now());
        reviewsDTO.setUpdatedAt(LocalDateTime.now());
        
        reviewsService.addReviews(reviewsDTO);
        log.info("用户 {} 添加评价成功, bookingId: {}", currentUserId, reviewsDTO.getBookingId());
        return Result.success();
    }

    @GetMapping("/showAllReviewsByUserId")
    public List<ReviewsDTO> showAllReviewsByUserId(){
        Integer userId = UserContext.getCurrentUserId();
        return reviewsService.showAllReviewsByUserId(userId);
    }

    @DeleteMapping("/deleteReviews")
    public Result<Void> deleteReviews(@RequestParam Integer id){
        if (id == null || id <= 0) {
            throw new CustomException("评价ID不合法");
        }
        
        Integer currentUserId = UserContext.getCurrentUserId();
        reviewsService.deleteReviews(id, currentUserId);
        log.info("用户 {} 删除评价成功, reviewId: {}", currentUserId, id);
        return Result.success();
    }

    @PutMapping("/updateReviews")
    public Result<Void> updateReviews(@RequestBody ReviewsDTO reviewsDTO){
        // 参数校验
        if (reviewsDTO == null || reviewsDTO.getId() == null) {
            throw new CustomException("评价ID不能为空");
        }
        if (reviewsDTO.getRating() == null || reviewsDTO.getRating() < 1 || reviewsDTO.getRating() > 5) {
            throw new CustomException("评分必须在1-5之间");
        }
        if (reviewsDTO.getContent() == null || reviewsDTO.getContent().trim().isEmpty()) {
            throw new CustomException("评价内容不能为空");
        }
        if (reviewsDTO.getContent().length() < 10 || reviewsDTO.getContent().length() > 500) {
            throw new CustomException("评价内容长度必须在10-500个字符之间");
        }
        
        Integer currentUserId = UserContext.getCurrentUserId();
        reviewsDTO.setUpdatedAt(LocalDateTime.now());
        
        reviewsService.updateReviews(reviewsDTO, currentUserId);
        log.info("用户 {} 修改评价成功, reviewId: {}", currentUserId, reviewsDTO.getId());
        return Result.success();
    }

    @GetMapping("/findByPropertyId")
    public List<ReviewsDTO> findByPropertyId(@RequestParam Integer propertyId){
        if (propertyId == null || propertyId <= 0) {
            throw new CustomException("房屋ID不合法");
        }

        return reviewsService.findByPropertyId(propertyId);
    }
}
